package com.hhh.petadopt.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhh.petadopt.mapper.AdminNewsMapper;
import com.hhh.petadopt.pojo.News;
import com.hhh.petadopt.pojo.NewsVo;
import com.hhh.petadopt.service.AdminNewsService;
import com.hhh.petadopt.utils.Page;

@Service
public class AdminNewsServiceImpl implements AdminNewsService {
	
	@Autowired
	private AdminNewsMapper adminnewsmapper;

	//分页
	@Override
	public Page<News> selectpage(NewsVo Vo) {
		//创建page对象
		Page<News> page = new Page<News>();
		//设置每页显示的条数
		page.setSize(10);
		Vo.setSize(10);
		if(null != Vo) {
			//判断当前页是否为空
			if(null != Vo.getPage()) {
				//设置当前页和起始页
				page.setPage(Vo.getPage());
				Vo.setStartRow((Vo.getPage()-1)*Vo.getSize());
			}
			//标题
			if(null != Vo.getNtitle() && !"".equals(Vo.getNtitle().trim())) {
				Vo.setNtitle(Vo.getNtitle().trim());
			}
			if(null != Vo.getNwriter() && !"".equals(Vo.getNwriter().trim())) {
				Vo.setNwriter(Vo.getNwriter().trim());
			}
			if(null != Vo.getNishot() &&!"".equals(Vo.getNishot().trim())) {
				Vo.setNishot(Vo.getNishot().trim());
			}
			if(null != Vo.getNisshow()&&!"".equals(Vo.getNisshow().trim())) {
				Vo.setNisshow(Vo.getNisshow().trim());
			}
			//封装符合条件的总条数
			page.setTotal(adminnewsmapper.selectTotal(Vo));
			List<News> selectNewsByVo = adminnewsmapper.selectNewsByVo(Vo);
			List<News> nList = new ArrayList<News>();
			for (News news : selectNewsByVo) {
				news.setCreated(news.getCreated().substring(0, 10));
				nList.add(news);
			}
			page.setRows(nList);
		}
		return page;
	}

	//添加
	@Override
	public void addnewsss(News n) {
		//补全信息
		n.setCreatedd(new Date());
		n.setUpdated(new Date());
		adminnewsmapper.addnewsss(n);
		
	}

	//信息回显
	@Override
	public News selectNewsById(Integer id) {
		//执行
		News news = adminnewsmapper.selectNewsById(id);
		return news;
	}

	//修改信息
	@Override
	public void uptdateNews(News n) {
		// TODO Auto-generated method stub
		//补全信息
		n.setCreatedd(new Date());
		n.setUpdated(new Date());
		adminnewsmapper.updateNews(n);
	}

	//删除
	@Override
	public void deleteNewsById(Integer id) {
		adminnewsmapper.deleteNewsById(id);
	}

	@Override
	public void deleteNewsByIds(Integer[] ids) {
		adminnewsmapper.deleteNewsByIds(ids);
		
	}

}
